package com.ybxx.base.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.ybxx.base.dao.RoomDao;
import com.ybxx.base.model.Community;
import com.ybxx.base.model.Room;
import com.ybxx.base.model.Unit;
import com.ybxx.base.service.RoomService;
import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.dao.GenericDao;
import com.ybxx.core.service.impl.BaseServiceImpl;
import com.ybxx.core.web.paging.PagingBean;

public class RoomServiceImpl extends BaseServiceImpl<Room> implements
		RoomService {
	private RoomDao roomDao;
	public RoomServiceImpl(RoomDao dao) {
		super(dao);
		this.roomDao=dao;
		// TODO Auto-generated constructor stub
	}
	public List<Room> queryTree(String uid, String loginid){
		String hql="select distinct r from Room as r,DataRights m where r.rid=m.dvalue and m.dtype='R' and r.unit.uid='"+uid+"' and m.user.loginid='"+loginid+"'";
		hql+=" order by r.rcode";
		return this.roomDao.findByHql(hql, null);
	}
	/*为打印通知单，查询相应单元下的房屋编号*/
	public List<Room> queryRoomRid(String unitid,String ymonth,String loginid){
		String condition="";
		if(unitid.indexOf("C")==0){
			condition=" and c.cid='"+unitid.substring(1)+"'";
		}
		if(unitid.indexOf("B")==0){
			condition=" and b.bid='"+unitid.substring(1)+"'";
		}
		if(unitid.indexOf("U")==0){
			condition=" and u.uid='"+unitid.substring(1)+"'";
		}
		if(unitid.indexOf("R")==0){
			condition=" and r.rid='"+unitid.substring(1)+"'";
		}
		String sql="select a.rid from " +
				"(select r.* from tbl_base_room r,tbl_base_unit u,tbl_base_ban b,tbl_base_community c," +
				"tbl_base_room_info ri,tbl_base_residents res,tbl_system_datarights dr " +
				"where dr.dtype='R' and dr.dvalue=r.rid and dr.loginid='"+loginid+"' and r.uid=u.uid and u.bid=b.bid and b.cid=c.cid and ri.rid=r.rid and res.rid=r.rid "+condition+") a left join " +
				"(select * from tbl_fees_records where ymonth='"+ymonth+"') b " +
				"on a.rid=b.rid";
		return roomDao.queryRoomRid(sql);
	}
	
}
